package modelnew;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;


public class Board {
	
	// Data Members
	private Part[][] m_boardParts;
	private int m_boardSize;
	
	// Constructors
	
	public Board(int boardSize, int polygonRadius) {
		m_boardSize = boardSize;
		
		// set up the board parts
		m_boardParts = new Part[boardSize][boardSize];
		for (int i = 0; i < boardSize; i++) {
			for (int j = 0; j < boardSize; j++) {
				if ((i+j)%2 == 0) {
					// set up the polygon points
					Point[] polygon = new Point[6];
					for (int k = 0; k < 6; k++) {
						int x = (int)(polygonRadius*Math.cos(k*Math.PI/6 * 2) + j*1.65*polygonRadius);
						int y = (int)(polygonRadius*Math.sin(k*Math.PI/6 * 2) + i*polygonRadius);
						polygon[k] = new Point(x, y);
					}
					
					// create the part in this place
					m_boardParts[i][j] = new Part(polygon);
				}
			}
		}
	}

	// Public Methods
	
	/**
	 * Gets the part that contains the point, or null if 
	 * it's not in the board
	 * @param point The point to find in the board
	 * @return The part that contains the point
	 */
	public Part getContainingPart(Point point) {
		Part foundPart = null;
		
		// Go over all of the parts and check if the point is there
		for (int i = 0; (i < m_boardSize) && (foundPart == null); i++) {
			for (int j = 0; (j < m_boardSize) && (foundPart == null); j++) {
				if (m_boardParts[i][j].containsPoint(point)) {
					foundPart = m_boardParts[i][j];
				}
			}
		}
		
		return foundPart;
	}
	
	/**
	 * Gets all the parts in the board not yet taken.
	 * @return A list of not taken parts.
	 */
	public List<Part> getNotTakenParts() {
		List<Part> notTakenParts = new ArrayList<Part>();
		
		// add all the not taken parts
		for (int i = 0; i < m_boardSize; i++) {
			for (int j = 0; j < m_boardSize; j++) {
				if (!m_boardParts[i][j].isTaken()) {
					notTakenParts.add(m_boardParts[i][j]);
				}
			}
		}
		
		return notTakenParts;
	}
	
	/**
	 * Checks if there is a part still available
	 * @return True if there is a part available.
	 */
	public boolean anyPartAvailable() {
		return (getNotTakenParts().size() > 0);
	}
}
